package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: OrderSettingServiceImpl
 * @Author: LiuJunMeng
 * @Date: 2020/1/14 18:38
 * @Description: //TODO
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 添加预约数据
     *
     * @param orderSettingList
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        //判断数据是否已存在
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {//如果存在,更新
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 查询预约信息
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //设置查询条件
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        Map dateMap = new HashMap();
        dateMap.put("dateBegin", dateBegin);
        dateMap.put("dateEnd", dateEnd);
        //调用方法查询预设数据,返回预设数据的集合
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(dateMap);
        List<Map> data = new ArrayList<>();
        //遍历预设对象集合,提取所需数据,封装到map集合中
        for (OrderSetting orderSetting : orderSettingList) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());//获得可用日期
            orderSettingMap.put("number", orderSetting.getNumber());//获取可预约人数
            orderSettingMap.put("reservations", orderSetting.getReservations());//获取已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    /**
     * 根据指定日期设置可预约人数
     *
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询当日是否已经设置预约人数
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //如果已经设置进行修改
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //如果没设置进行添加
            orderSettingDao.add(orderSetting);
        }
    }

    /**
     * 删除历史预约数据
     *
     * @param time
     */
    @Override
    public void clearOrderSetting(String time) {
        orderSettingDao.clearOrderSetting(time);
    }
}
